package com.kkb.kkbportal.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.kkbcommon.api.CommonPage;
import com.kkb.kkbcommon.api.CommonResult;
import com.kkb.kkbportal.domain.ums.Permission;
import com.kkb.kkbportal.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "permissionController", description = "权限管理")
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "权限信息分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "conditionalPermission", value = "权限筛选条件", dataType = "Permission"),
            @ApiImplicitParam(name = "pageNum", value = "当前页数", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页页数", defaultValue = "10", dataType = "int")
    })
    @PostMapping("/query/page")
    public CommonResult<CommonPage<Permission>> listPage(@RequestBody Permission conditionalPermission,
                                                         @RequestParam(defaultValue = "1") Integer pageNum,
                                                         @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Permission> page = permissionService.selectPage(conditionalPermission, pageNum, pageSize);
        CommonPage<Permission> commonPage = CommonPage.restPage(page.getList());
        return CommonResult.success(commonPage, "查询成功");
    }

    @ApiOperation(value = "根据id得到权限信息")
    @ApiImplicitParam(name = "id", value = "权限id", dataType = "long")
    @GetMapping("/{id}}")
    public CommonResult<Permission> selectPermissionById(@PathVariable("id") Long id) {
        Permission permission = permissionService.selectPermissionById(id);
        return CommonResult.success(permission, "查询成功");
    }

    @ApiOperation(value = "新增权限")
    @ApiImplicitParam(name = "permission", value = "权限对象")
    @PutMapping("/add")
    public CommonResult<Permission> addPermission(@RequestBody Permission permission) {
        permissionService.insert(permission);
        return CommonResult.success(null, "添加成功");
    }

    @ApiOperation(value = "根据权限id修改权限")
    @ApiImplicitParam(name = "permission", value = "权限对象")
    @PutMapping("/update")
    public CommonResult<Permission> updateRoleById(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return CommonResult.success(null, "修改成功");
    }

    @ApiOperation(value = "根据权限id进行逻辑删除", notes = "权限删除接口")
    @ApiImplicitParam(name = "id", value = "权限id", dataType = "long")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Permission> deletePermissionById(@PathVariable("id") Long id) {
        permissionService.deleteById(id);
        return CommonResult.success(null, "删除成功");
    }

    @ApiOperation(value = "权限信息列表查询", notes = "权限列表查询接口")
    @ApiImplicitParam(name = "conditionalPermission", value = "权限筛选条件", dataType = "Permission")
    @PostMapping("/query/list")
    public CommonResult<List<Permission>> list(@RequestBody Permission conditionalPermission) {
        List<Permission> page = permissionService.selectByCondition(conditionalPermission);
        System.out.println(page);
        return CommonResult.success(page, "查询成功");
    }
}
